//
//  WmcHomeVideoBtn.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/7/7.
//

import UIKit

extension WmcHomeVideoBtn {
    /// 状态
    enum State {
        /// 正常
        case normal
        /// 录制中
        case recording
    }
}

// MARK: - [WmcHomeVideoBtn]

class WmcHomeVideoBtn: WmcBaseControl {
    lazy var centerRedView: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = false
        view.backgroundColor = .wmc_red_F54A45
        view.layer.masksToBounds = true
        return view
    }()

    lazy var normalLineView: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = false
        view.layer.borderWidth = 4
        view.layer.masksToBounds = true
        return view
    }()

    lazy var progressLayer: WmcCircularProgressLayer = {
        let layer = WmcCircularProgressLayer()
        layer.strokeColor = UIColor.white.cgColor
        return layer
    }()

    let normalSize: CGFloat = 56
    let recordingSize: CGFloat = 24

    
    var roundLineColor: UIColor = .white
    
    /// 设置圆线色
    func setLineColor(_ color: UIColor) {
        roundLineColor = color
        normalLineView.layer.borderColor = color.cgColor
        progressLayer.strokeColor = color.cgColor
    }
    
    override func makeUI() {
        backgroundColor = .clear

        addSubview(normalLineView)
        normalLineView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        addSubview(centerRedView)
        centerRedView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(normalSize)
        }

        layer.addSublayer(progressLayer)

        setState(.normal)
        
        setLineColor(.white)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        // 正常状态外部圆环
        normalLineView.layer.cornerRadius = normalLineView.bounds.size.width / 2
        // 进度圆环
        progressLayer.frame = bounds
    }

    func setState(_ state: State) {
        setNeedsUpdateConstraints()
        UIView.animate(withDuration: 0.3) { [weak self] in
            guard let weakSelf = self else { return }
            switch state {
            case .normal:
                weakSelf.normalLineView.layer.borderColor = weakSelf.roundLineColor.cgColor
                weakSelf.centerRedView.snp.updateConstraints { make in
                    make.size.equalTo(weakSelf.normalSize)
                }
                weakSelf.centerRedView.layer.cornerRadius = weakSelf.normalSize / 2
                
            case .recording:
                weakSelf.normalLineView.layer.borderColor = weakSelf.roundLineColor.yp.alpha(0.5).cgColor
                weakSelf.centerRedView.snp.updateConstraints { make in
                    make.size.equalTo(weakSelf.recordingSize)
                }
                weakSelf.centerRedView.layer.cornerRadius = 4
            }
            weakSelf.layoutIfNeeded()
        }
    }

    func setProgress(_ progress: CGFloat) {
        progressLayer.setProgress(progress)
    }
}
